package com.smartphonedev.files;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.smartphonedev.activity.R;

import android.content.Context;
import android.util.Log;

public class FileUtils {
	public static String getTextFromFile(String fileName, Context context) {
		try {
			StringBuilder text = new StringBuilder();
			File f = new File(context.getString(R.string.fordername) + fileName);
			if (f.exists()) {
				BufferedReader br = new BufferedReader(new FileReader(f));
				String line;
				while ((line = br.readLine()) != null) {
					text.append(line);
					text.append('\n');
				}
			}
			return new String(text);
		} catch (Exception e) {
			//System.err.println(e.getStackTrace().toString());
			Log.e("toi dang doc file", e.getStackTrace().toString());
			return "";
		}
	}

	public static String getTextFromFileVerification(String fileName,
			Context context) {
		try {
			FileInputStream fIn = context.openFileInput(fileName);
			InputStreamReader isr = new InputStreamReader(fIn);
			/*
			 * Prepare a char-Array that will hold the chars we read back in.
			 */
			char[] inputBuffer = new char[32];
			isr.read(inputBuffer);
			return new String(inputBuffer);
		} catch (Exception e) {
			System.err.println(e.getStackTrace().toString());
			return "";
		}
	}

	public static void taoFileTXT(String noidung, String fileName,
			Context context) {
		try {
			// catches IOException below
			final String TEXTSTRING = new String(noidung);
			FileOutputStream fOut = context.openFileOutput(fileName,
					Context.MODE_WORLD_WRITEABLE
					);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);
			// Write the string to the file
			osw.write(TEXTSTRING);
			/*
			 * ensure that everything is really written out and close
			 */
			osw.flush();
			osw.close();
		} catch (Exception ioe) {
			ioe.printStackTrace();
		}
	}

	public static void addLineToFile(String line, String fileName,
			Context context) {

		try {
			// catches IOException below
			String filePath = context.getResources().getString(
					R.string.fordername);
			File folder = new File(filePath);
			if (!folder.exists()) {
				folder.mkdirs();
			}

			File f = new File(filePath + fileName);
			final String TEXTSTRING = new String(line);
			FileWriter fOut = new FileWriter(f, true);
			// Write the string to the file
			fOut.append(TEXTSTRING);
			fOut.append("\n");
			fOut.flush();
			fOut.close();
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	public static void deleteFile(String fileName, Context context) {

		// catches IOException below
		String filePath = context.getResources().getString(
				R.string.fordername);
		File folder = new File(filePath);
		if (!folder.exists()) {
			folder.mkdirs();
		}

		File f = new File(filePath + fileName);
		if (f.exists()) {
			f.delete();
		}
	}

}
